﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Core.General {

    public class GeneralFactory  : FactoryBase {

        #region Constructors

        public GeneralFactory (Application forApplication) : base (forApplication) { return; }

        #endregion 
        

        #region Factories

        public List<ActionReason> ActionReasons (Boolean useCaching = false) {

            List<ActionReason> sponsorTypes = new List<ActionReason> ();

            String cacheKey = "Application.ActionReasons.All";

            ClearLastException ();

            try {

                if (useCaching) { sponsorTypes = (List<ActionReason>)CacheManager.GetObject (cacheKey); }

                else { sponsorTypes = null; }

                if (sponsorTypes == null) {

                    sponsorTypes = new List<ActionReason> ();

                    String selectStatement = "SELECT ActionReason.* ";

                    selectStatement += "  FROM ActionReason";

                    selectStatement += "  ORDER BY ActionReason.Name";


                    System.Data.DataTable dataTable = Application.EnvironmentDatabase.SelectDataTable (selectStatement);

                    foreach (System.Data.DataRow currentDataRow in dataTable.Rows) {

                        ActionReason sponsorType = new ActionReason (Application);

                        sponsorType.MapDataFields (currentDataRow);

                        CacheManager.CacheObject ("Application.ActionReason." + sponsorType.Id, sponsorType, CacheExpirationReference);  // CACHE INDIVIDUAL ROLE 

                        sponsorTypes.Add (sponsorType);

                    }

                    CacheManager.CacheObject (cacheKey, sponsorTypes, CacheExpirationReference); // CACHE THE WHOLE LIST

                }

            }

            catch (Exception applicationException) {

                SetLastException (applicationException);

            }

            return sponsorTypes;

        }

        public ActionReason ActionReason (Int64 id, Boolean useCaching = false) {

            if (id == 0) { return null; }

            String cacheKey = "Physis.Application.ActionReason." + id;

            ActionReason sponsorType = null;

            ClearLastException ();

            try {

                if (useCaching) { sponsorType = (ActionReason)CacheManager.GetObject (cacheKey); }

                if (sponsorType == null) {

                    sponsorType = new ActionReason (Application, id);

                    CacheManager.CacheObject (cacheKey, sponsorType, CacheExpirationReference);

                }

            }

            catch (Exception applicationException) {

                SetLastException (applicationException);

            }

            return sponsorType;

        }

        public ActionReason ActionReason (String name, Boolean useCaching = false) {

            if (String.IsNullOrWhiteSpace (name)) { return null; }

            String cacheKey = "Physis.Application.ActionReason." + name;

            ActionReason sponsorType = null;

            ClearLastException ();

            try {

                if (useCaching) { sponsorType = (ActionReason)CacheManager.GetObject (cacheKey); }

                if (sponsorType == null) {

                    sponsorType = new ActionReason (Application, name);

                    CacheManager.CacheObject (cacheKey, sponsorType, CacheExpirationReference);

                }

            }

            catch (Exception applicationException) {

                SetLastException (applicationException);

            }

            return sponsorType;

        }

        #endregion 

    }

}
